<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/16 13:38
 * Note: 文章管理
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController {
    //分类列表
    public function index(){
        $Article = M('Article'); // 实例化User对象
        $count = $Article->where(array('status'=>'1'))->count();// 查询满足要求的总记录数
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list  = $Article->where('status=1')->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as &$v){
            $v['time'] = date("Y-m-d H:i:s",$v['time']);
            $v['catename'] = M('Cate')->where(array('id'=>$v['cateid']))->getField('title');
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('title', '文章列表');
        $this->display();
    }
    //新增文章
    public function plus(){
        $cate = M('Cate')->where(array('status'=>'1','type'=>'0'))->select();
        if(IS_POST){
            $title = I('post.title','','trim');
            if(M('Article')->where(array('title'=>$title))->select()){
                $this->error('文章标题已存在！', U('Article/plus'));
            }
            if(!$title){
                $this->error('请填写文章标题！', U('Article/plus'));
            }
            $author  = I('post.author','','trim');
            $author  = !empty($author) ? $author : 'admin';
            $cateid  = I('post.cateid','','intval');
            if(!$cateid){
                $this->error('请选择文章分类！', U('Article/plus'));
            }
            $desc    = I('post.desc','','trim');//文章来源
            $content = I('post.content','','trim');
            $orderid = I('post.orderid','','intval');
            $url  = I('post.url','','trim');
            $data = array(
                'title'   => $title ,
                'author'  => $author ,
                'desc'    => $desc ,
                'content' => $content ,
                'cateid'  => $cateid ,
                'orderid' => $orderid ,
                'url'     => $url ,
                'status'  => '1' ,
                'time'    => time() ,
            );
           // print_r($data);die();
            $result = M('Article')->add($data);
            // print_r($result);die();
            if(!$result){
                $this->error('新增失败', U('Article/index'));
            }else{
                $this->success('新增成功', U('Article/index'));
            }
            return;
        }
        $this->assign('cate', $cate);
        $this->assign('title', '新增分类');
        $this->display();
    }
    //文章修改
    public function edit(){
        $id = I('get.id','','intval');
        $cate = M('Cate')->where(array('status'=>'1','type'=>'0'))->select();
        $info = M('Article')->where(array('id'=>$id))->find();
        if(IS_POST){
            $id = I('post.id','','intval');
            $title = I('post.title','','trim');
            if(!$title){
                $this->error('请填写文章标题！', U('Article/index'));
            }
            $author = I('post.author','','trim');
            $cateid = I('post.cateid','','intval');
            if(!$cateid){
                $this->error('请选择文章分类！', U('Article/index'));
            }
            $desc   = I('post.desc','','trim');
            $content = I('post.content','','trim');
            $orderid = I('post.orderid','','intval');
            $url  = I('post.url','','trim');
            $data = array(
                'title'   => $title ,
                'author'  => !empty($author) ? $author : 'admin',
                'desc'    => $desc ,
                'content' => $content ,
                'cateid'  => $cateid ,
                'orderid' => $orderid ,
                'url'     => $url ,
                'status'  => '1' ,
                'time'    => time() ,
            );
            $result = M('Article')->where(array('id'=>$id))->save($data);
            if(!$result) {
                $this->error('更新失败！', U('Article/edit'));
            }else{
                $this->success('更新成功', U('Article/index'));
            }
            return;
        }
        $this->assign('cate', $cate);
        $this->assign('info', $info);
        $this->assign('title', '文章修改');
        $this->display();
    }
    //文章删除
    public function del(){
        $id = I('get.id','','intval');
        $result = M('Article')->where(array('id'=>$id))->save(array('status'=>'0'));
        if(!$result){
            $this->error('删除失败', U('Article/index'));
        }else{
            $this->success('删除成功', U('Article/index'));
        }
        return;
    }
    //分类排序
    public function sort(){
        foreach($_POST as $id=>$sort){
            M('Article')->where(array('id'=>$id))->setField('sort',$sort);
        }
        $this->success('新增排序成功', U('Article/index'));
    }
}